package com.tj.jinzao.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.tj.jinzao.model.Health;
import com.tj.jinzao.model.LineDataBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuYX on 2019/5/13.
 */

public class ChartLineView extends View{
    Path mPath;
    Paint mPaint;
    Paint mPaintTxt;
    Paint mPaintCenter;
    private Context mContext;
    private int mWithDisplay=1080;
    private int mHeightDisplay=1920;

    public ChartLineView(Context context) {
        super(context);
        mContext=context;
        init();
    }

    public ChartLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init();
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public ChartLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        init();
    }
    private void init(){

        mPath=new Path();
        mPaint=new Paint();
        mPaintCenter=new Paint();
        mPaintTxt=new Paint();
        mPaint.setStrokeWidth(2);
        mPaintCenter.setStrokeWidth(2);

        mPaint.setColor(Color.parseColor("#DADCE7"));
        mPaintTxt.setColor(Color.parseColor("#838699"));
        mPaintCenter.setColor(Color.parseColor("#FCEBDE"));
        mPaintTxt.setTextSize(dip2px(getContext(), 12));
        //设置抗锯齿
        mPaintCenter.setAntiAlias(true);
        mPaintCenter.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaintTxt.setAntiAlias(true);
        mPaintTxt.setStyle(Paint.Style.STROKE);
        avgX=(endX-startX)/28;
        avgY=(endY-startY)/10;
        pos=0;

    };
    private int pos;
    private List<LineDataBean> list=new ArrayList<>();
    public void setData(List<LineDataBean> list){
        this.list=list;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++){
                if(list.get(i).isReset()){
                    mPath.moveTo(list.get(i).getX(),list.get(i).getY());
                }else {
                    if(list.get(i).ismCenter()){
                        canvas.drawLine(list.get(i-1).getX(),list.get(i-1).getY(),list.get(i).getX(),list.get(i).getY(),mPaintCenter);
                    }else {
                        mPath.lineTo(list.get(i).getX(),list.get(i).getY());
                    }
                }
                if(list.get(i).isTxt()){
                    canvas.drawText(list.get(i).getTxtContent(),list.get(i).getTxtX(),list.get(i).getTxtY(),mPaintTxt);
                }
            }
        }
        canvas.drawPath(mPath,mPaint);

    }
    float width;
    float height;
    private int startX=80,startY=40,endX=640,endY=300;
    private int avgX,avgY,center=10;
    private int getWidth(int x){
        return (int) (width/720*x);
    }
    private int getHeight(int y){
        return (int) (height/1280*y);
    }

    public void init(float width,float height,List<Health> list_h) {
        this.width=width;
        this.height=height;
        list.clear();
        LineDataBean lineDataBean=new LineDataBean();
        lineDataBean.setReset(true);
        lineDataBean.setX(getWidth(startX));
        lineDataBean.setY(getHeight(startY));
        list.add(lineDataBean);
        LineDataBean lineDataBean1=new LineDataBean();
        lineDataBean1.setReset(false);
        lineDataBean1.setX(getWidth(startX));
        lineDataBean1.setY(getHeight(endY));
        list.add(lineDataBean1);
        LineDataBean lineDataBean2=new LineDataBean();
        lineDataBean2.setReset(false);
        lineDataBean2.setX(getWidth(endX));
        lineDataBean2.setY(getHeight(endY));
        list.add(lineDataBean2);
        LineDataBean lineDataBean3=new LineDataBean();
        lineDataBean3.setReset(false);
        lineDataBean3.setX(getWidth(endX));
        lineDataBean3.setY(getHeight(startY));
        list.add(lineDataBean3);
        LineDataBean lineDataBean4=new LineDataBean();
        lineDataBean4.setReset(false);
        lineDataBean4.setX(getWidth(startX));
        lineDataBean4.setY(getHeight(startY));
        list.add(lineDataBean4);
        //绘制竖直锯齿
        for (int i=0;i<11;i++){
            LineDataBean lineDataBean5=new LineDataBean();
            lineDataBean5.setReset(true);
            lineDataBean5.setX(getWidth(startX));
            lineDataBean5.setY(getHeight(startY+avgY*i));
            list.add(lineDataBean5);
            LineDataBean lineDataBean6=new LineDataBean();
            lineDataBean6.setReset(false);
            if(i==4){
                lineDataBean6.setX(getWidth(startX-center-5));
            }else {
                lineDataBean6.setX(getWidth(startX-center));
            }
            if(i==0){
                lineDataBean6.setTxt(true);
                lineDataBean6.setTxtX(getWidth(startX-center-45));
                lineDataBean6.setTxtY(getHeight(startY+avgY*i+10));
                lineDataBean6.setTxtContent(String.valueOf(100-10*i));
            }else if(i==2||i==4){
                lineDataBean6.setTxt(true);
                lineDataBean6.setTxtX(getWidth(startX-center-32));
                lineDataBean6.setTxtY(getHeight(startY+avgY*i+10));
                lineDataBean6.setTxtContent(String.valueOf(100-10*i));
            }else if(i==10){
                lineDataBean6.setTxt(true);
                lineDataBean6.setTxtX(getWidth(startX-center-22));
                lineDataBean6.setTxtY(getHeight(startY+avgY*i+10));
                lineDataBean6.setTxtContent(String.valueOf(100-10*i));
            }
            lineDataBean6.setY(getHeight(startY+avgY*i));
            list.add(lineDataBean6);
        }
        //横向平分多少次
        int k=0;
        if(list_h.size()>5){
            k=5;
        }else if(list_h.size()==2){
            k=14;
        }else if(list_h.size()==3){
            k=10;
        }else if(list_h.size()==4){
            k=7;
        }
        //绘制水平锯齿
        for (int i=0;i<29;i++){
            if(list_h.size()==1){
                if(pos>1){
                    getDefault(i);
                    continue;
                }
                LineDataBean lineDataBean5=new LineDataBean();
                lineDataBean5.setReset(true);
                lineDataBean5.setX(getWidth(startX+avgX*i));
                lineDataBean5.setY(getHeight(endY));
                list.add(lineDataBean5);
                LineDataBean lineDataBean6=new LineDataBean();
                lineDataBean6.setReset(false);
                lineDataBean6.setX(getWidth(startX+avgX*i));
                lineDataBean6.setY(getHeight(endY+center+6));
                lineDataBean6.setTxt(true);
                lineDataBean6.setTxtY(getHeight(endY+center+32));
                if(pos==1){
                    lineDataBean6.setTxtX(getWidth(startX+avgX*28-50));
                }else {
                    if((8+pos)<10){
                        lineDataBean6.setTxtX(getWidth(startX+avgX*i-20));
                    }else {
                        lineDataBean6.setTxtX(getWidth(startX+avgX*i-26));
                    }
                }

                int month=Integer.parseInt(list_h.get(0).getReportTime().substring(5,7));
                int day=Integer.parseInt(list_h.get(0).getReportTime().substring(8,10));
                String data="";
                if(month<10){
                    data="0"+month;
                }else {
                    data=""+month;
                }
                if(day<10){
                    data=data+"-0"+day;
                }else {
                    data=data+"-"+day;
                }
                lineDataBean6.setTxtContent(data);
                list.add(lineDataBean6);
                pos++;
            }else if(i!=0&&i%k==0){
                LineDataBean lineDataBean5=new LineDataBean();
                lineDataBean5.setReset(true);
                lineDataBean5.setX(getWidth(startX+avgX*i));
                lineDataBean5.setY(getHeight(endY));
                list.add(lineDataBean5);
                LineDataBean lineDataBean6=new LineDataBean();
                lineDataBean6.setReset(false);
                lineDataBean6.setX(getWidth(startX+avgX*i));
                lineDataBean6.setY(getHeight(endY+center+6));
                lineDataBean6.setTxt(true);
                lineDataBean6.setTxtY(getHeight(endY+center+32));
                if((8+pos)<10){
                    lineDataBean6.setTxtX(getWidth(startX+avgX*i-20));
                }else {
                    lineDataBean6.setTxtX(getWidth(startX+avgX*i-26));
                }
                if(pos==0){
                    if(list_h.size()<1){
                        getDefault(i);
                        continue;
                    }

                    int month=Integer.parseInt(list_h.get(0).getReportTime().substring(5,7));
                    int day=Integer.parseInt(list_h.get(0).getReportTime().substring(8,10));
                    String data="";
                    if(month<10){
                        data="0"+month;
                    }else {
                        data=""+month;
                    }
                    if(day<10){
                        data=data+"-0"+day;
                    }else {
                        data=data+"-"+day;
                    }
                    lineDataBean6.setTxtContent(data);
                    list.add(lineDataBean6);

                }else if(pos==1){
                    if(list_h.size()<2){
                        getDefault(i);
                        continue;
                    }
                    int month1=Integer.parseInt(list_h.get(1).getReportTime().substring(5,7));
                    int day1=Integer.parseInt(list_h.get(1).getReportTime().substring(8,10));
                    String data1="";
                    if(month1<10){
                        data1="0"+month1;
                    }else {
                        data1=""+month1;
                    }
                    if(day1<10){
                        data1=data1+"-0"+day1;
                    }else {
                        data1=data1+"-"+day1;
                    }
                    lineDataBean6.setTxtContent(data1);
                    list.add(lineDataBean6);

                }else if(pos==2){
                    if(list_h.size()<3){
                        getDefault(i);
                        continue;
                    }

                    int month2=Integer.parseInt(list_h.get(2).getReportTime().substring(5,7));
                    int day2=Integer.parseInt(list_h.get(2).getReportTime().substring(8,10));
                    String data2="";
                    if(month2<10){
                        data2="0"+month2;
                    }else {
                        data2=""+month2;
                    }
                    if(day2<10){
                        data2=data2+"-0"+day2;
                    }else {
                        data2=data2+"-"+day2;
                    }
                    lineDataBean6.setTxtContent(data2);
                    list.add(lineDataBean6);
                }else if(pos==3){
                    if(list_h.size()<4){
                        getDefault(i);
                        continue;
                    }

                    int month3=Integer.parseInt(list_h.get(list_h.size()>6?list_h.size()/2-1:2).getReportTime().substring(5,7));
                    int day3=Integer.parseInt(list_h.get(list_h.size()>6?list_h.size()/2-1:2).getReportTime().substring(8,10));
                    String data3="";
                    if(month3<10){
                        data3="0"+month3;
                    }else {
                        data3=""+month3;
                    }
                    if(day3<10){
                        data3=data3+"-0"+day3;
                    }else {
                        data3=data3+"-"+day3;
                    }
                    lineDataBean6.setTxtContent(data3);
                    list.add(lineDataBean6);

                }else if(pos==4){
                    if(list_h.size()<5){
                        getDefault(i);
                        continue;
                    }

                    int month4=Integer.parseInt(list_h.get(list_h.size()-1).getReportTime().substring(5,7));
                    int day4=Integer.parseInt(list_h.get(list_h.size()-1).getReportTime().substring(8,10));
                    String data4="";
                    if(month4<10){
                        data4="0"+month4;
                    }else {
                        data4=""+month4;
                    }
                    if(day4<10){
                        data4=data4+"-0"+day4;
                    }else {
                        data4=data4+"-"+day4;
                    }
                    lineDataBean6.setTxtContent(data4);
                    list.add(lineDataBean6);

                }
                pos++;
            }else {
                getDefault(i);

            }

        }
        LineDataBean lineDataBean5=new LineDataBean();
        lineDataBean5.setReset(true);
        lineDataBean5.setX(getWidth(startX));
        lineDataBean5.setY(getHeight(startY+avgY*4));
        list.add(lineDataBean5);
        LineDataBean lineDataBean6=new LineDataBean();
        lineDataBean6.setReset(false);
        lineDataBean6.setmCenter(true);
        lineDataBean6.setX(getWidth(endX));
        lineDataBean6.setY(getHeight(startY+avgY*4));
        list.add(lineDataBean6);
        invalidate();
    }
    private void getDefault(int i){
        LineDataBean lineDataBean5=new LineDataBean();
        lineDataBean5.setReset(true);
        lineDataBean5.setX(getWidth(startX+avgX*i));
        lineDataBean5.setY(getHeight(endY));
        list.add(lineDataBean5);
        LineDataBean lineDataBean6=new LineDataBean();
        lineDataBean6.setReset(false);
        lineDataBean6.setX(getWidth(startX+avgX*i));
        lineDataBean6.setY(getHeight(endY+center));
        list.add(lineDataBean6);

    }

}
